﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Xml.Linq;

namespace VisualRibbonEditor
{
	public partial class MainWindow : Window
	{
		public void ClearServerVersionStatus()
		{
			SetServerVersionStatus(new Version());
		}
		public void SetServerVersionStatus(Version version)
		{
			string text = String.Empty;
			string toolTip = String.Empty;
			string v = String.Empty;
			string ur = String.Empty;
			switch (version.Major)
			{
				case 0:
					break;
				case 5:
					v = "2011";
					if (version.Build == 9585 && version.Revision == 101) ur = " Beta";
					if (version.Build == 9688 && version.Revision == 53) ur = " RC";
					if (version.Build == 9688 && version.Revision == 583) ur = " RTM";
					if (version.Build == 9688 && version.Revision == 1045) ur = " UR1";
					if (version.Build == 9688 && version.Revision == 1157) ur = " UR2";
					if (version.Build == 9688 && version.Revision == 1244) ur = " UR3";
					if (version.Build == 9688 && version.Revision == 1450) ur = " UR4";
					if (version.Build == 9688 && version.Revision == 1533) ur = " UR5";
					if (version.Build == 9689 && version.Revision == 1985) ur = " UR6";
					if (version.Build == 9690 && version.Revision == 1992) ur = " UR6+";
					if (version.Build == 9690 && version.Revision == 2165) ur = " UR7";
					if (version.Build == 9690 && version.Revision == 2243) ur = " UR8";
					if (version.Build == 9690 && version.Revision == 2730) ur = " UR10";
					if (version.Build == 9690 && version.Revision == 2740) ur = " UR10+";
					if (version.Build == 9690 && version.Revision == 2835) ur = " UR11";
					if (version.Build == 9690 && version.Revision == 2839) ur = " UR11+";
					if (version.Build == 9690 && version.Revision == 2903) ur = " UR11+CU";
					if (version.Build == 9690 && version.Revision == 3236) ur = " UR12";
					if (version.Build == 9690 && version.Revision == 3448) ur = " UR13";
					if (version.Build == 9690 && version.Revision == 3557) ur = " UR14";
					if (version.Build == 9690 && version.Revision == 3731) ur = " UR15";
					if (version.Build == 9690 && version.Revision == 3911) ur = " UR16";
					if (version.Build == 9690 && version.Revision == 4150) ur = " UR17";
					if (version.Build == 9690 && version.Revision == 4376) ur = " UR18";
					break;
				case 6:
					v = "2013";
					if (version.Minor == 0 && version.Build == 0 && version.Revision == 215) ur = " Beta";
					if (version.Minor == 0 && version.Build == 0 && version.Revision == 514) ur = " RC";
					if (version.Minor == 0 && version.Build == 0 && version.Revision == 809) ur = " RTM";
					if (version.Minor == 0 && version.Build == 1 && version.Revision == 61) ur = " UR1";
					if (version.Minor == 0 && version.Build == 2 && version.Revision == 46) ur = " UR2";
					if (version.Minor == 0 && version.Build == 2 && version.Revision == 51) ur = " UR2+";
					if (version.Minor == 1 && version.Build == 0 && version.Revision == 581) ur = " SP1";
					if (version.Minor == 1 && version.Build == 1 && version.Revision == 132) ur = " SP1 UR1";
					if (version.Minor == 1 && version.Build == 2 && version.Revision == 112) ur = " SP1 UR2";
					if (version.Minor == 1 && version.Build == 3 && version.Revision == 119) ur = " SP1 UR3";
					if (version.Minor == 1 && version.Build == 4 && version.Revision == 145) ur = " SP1 UR4";
					break;
				case 7:
					v = "2015";
					if (version.Minor == 0 && version.Build == 0 && version.Revision == 3543) ur = " RTM";
					if (version.Minor == 0 && version.Build == 1 && version.Revision == 129) ur = " UP 0.1";
					if (version.Minor == 0 && version.Build == 2 && version.Revision == 53) ur = " UP 0.2";
					break;
				case 8:
					v = "2016";
					if (version.Minor == 0 && version.Build == 0 && version.Revision == 0) ur = " RTM";
					if (version.Minor == 0 && version.Build == 1 && version.Revision == 79) ur = " UP 0.1";
					if (version.Minor == 1 && version.Build == 0 && version.Revision == 359) ur = " SP1";
					break;
				default:
					text = "Version: " + version.ToString();
					toolTip = text;
					break;
			}
			// Detection of future Service Pack and Update Rollup versions for CRM 2013, 2015 and 2016
			if (version.Major == 6 || version.Major == 7 || version.Major == 8)
			{
				if (String.IsNullOrWhiteSpace(ur))
				{
					if (version.Minor > 0) ur = " SP" + version.Minor.ToString();
					if (version.Build > 0) ur += " UR" + version.Build.ToString();
				}
			}
			if (version.Major > 0)
			{
				if (String.IsNullOrWhiteSpace(ur))
				{
					// Service Pack and Update Rollup versions were not detected
					ur = " (" + version.ToString() + ")";
				}
				else
				{
					toolTip = String.Format("Version: {0}{1}", v, ur) + " (" + version.ToString() + ")";
				}
				text = String.Format("Version: {0}{1}", v, ur);

				if (String.IsNullOrWhiteSpace(toolTip)) toolTip = text;
			}

			SbServerVersion.Text = text;
			SbServerVersion.ToolTip = toolTip;
		}
	}
}